var _xmlData ;

function searchBooks() {
	
	var bookSearch = Ext.get('book').getValue() ;
	//alert(bookSearch) ;
	// http://books.google.com/books/feeds/volumes
	var params = {
			q : bookSearch 
			
	}; 
	
	var encparam = gadgets.io.encodeValues(params) ;
	var URL = "http://books.google.com/books/feeds/volumes?" + encparam ; 
	
	var req = osapi.http.get({
		href : URL ,
		format: 'xml' 
	});
	
	req.execute(loadBooks) ;
	
}

function loadBooks(xmldata) {
	//alert(xmldata) ;
	_xmlData = xmldata ;
	
	 var parser=new DOMParser();
	 var xmlDoc=parser.parseFromString(xmldata.content, "text/xml");
	// alert(xmlDoc); 

	
	var gridstore = new Ext.data.XmlStore({
		storeId : 'gridstore', 
		record: 'entry' ,
		totalRecords: 3 ,
		fields: ['title', 'id' ,
		         {name:'Image' ,mapping: 'link[@rel="http://schemas.google.com/books/2008/thumbnail]/@href' }, 'creator' 
		         
		          
		         
		         ] 
	}) ;
	gridstore.loadData(xmlDoc) ; 
	
	var gridPanel = new Ext.grid.GridPanel({
		store: gridstore,
		columns: [ 
                   {header: "Image",  dataIndex:'Image', sortable: false, renderer:bookImage} ,
		           {header: "Title", width: 250, dataIndex:'title', sortable: false},  
                   {header: "Author", width: 250, dataIndex:'creator', sortable: false}   
		           
		         ],
		         
		width: 540 ,
		height: 200
		
	}) ;
	
	Ext.get('book-result-table').update('') ;
	//Ext.DomHelper.insertHtml('afterBegin' , Ext.get('book-result-table'), '') ;
	gridPanel.render('book-result-table') ;
	
//	alert(xmldata.content) ;
}
 

function bookImage(location) {
	if (location) {
		return '<img src="' + location + '" />' ; 
	}
	return "" ;
	
}


function loadFriends() {
    	 alert("Friends Loaded") ;
         var req = opensocial.newDataRequest();
    	 req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER), 'viewer');
    	 var viewerFriends = opensocial.newIdSpec({ "userId" : "VIEWER", "groupId" : "FRIENDS" });
    	 var opt_params = {};
    	 opt_params[opensocial.DataRequest.PeopleRequestFields.MAX] = 100;
    	 req.add(req.newFetchPeopleRequest(viewerFriends, opt_params), 'viewerFriends');
    	 req.send(onLoadFriends);
    	}
     function onLoadFriends(data) {
    	 alert("Making DOM Changes" + data) ; 
    	 var viewer = data.get('viewer').getData();
    	 var viewerFriends = data.get('viewerFriends').getData();
    	 //html = new Array();
    	 //html.push('<ul>');
    	 var friendHtml = new Array() ;
         //var friend-list = Ext.get('friend-list') ;  
    	 viewerFriends.each(function(person) {
    	   if (person.getId()) {
    		   friendHtml.push({tag:'li' , id:person.getId() , html:person.getDisplayName()}) ;
    		   alert(friendHtml.length); 
    	   }
    	  });
    	 var dh = Ext.DomHelper;
    	 alert(friendHtml) ; 
    	 dh.append('friend-list', friendHtml);  
    	}